package cn.example.vip.mall.page.service.impl;

import cn.example.vip.mall.goods.entity.Category;
import cn.example.vip.mall.goods.entity.Product;
import cn.example.vip.mall.goods.entity.Sku;
import cn.example.vip.mall.goods.entity.Spu;
import cn.example.vip.mall.goods.feign.CategoryFeign;
import cn.example.vip.mall.goods.feign.SkuFeign;
import cn.example.vip.mall.goods.feign.SpuFeign;
import cn.example.vip.mall.page.service.PageService;
import cn.example.vip.mall.util.RespResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {
	
	@Autowired
	TemplateEngine templateEngine;
	
	@Autowired
	CategoryFeign categoryFeign;
	
	@Autowired
	SpuFeign spuFeign;
	
	@Value("${items.pagepath}")
	private String pagepath;
	@Autowired
	private SkuFeign skuFeign;
	
	@Override
	public void html(String spuId) throws Exception {
		
		// 1.创建容器对象(上下文对象)
		Context context = new Context();
		// 2.设置模板数据
		context.setVariables(loadData(spuId));
		// 3.指定文件生成后的存储路径
		File file = new File(pagepath, spuId + ".html");
		
		// 获取类加载器
//		ClassLoader classLoader = PageServiceImpl.class.getClassLoader();
		// 获取资源文件所在目录的路径
//		String path = classLoader.getResource("static").getPath();
		// 创建文件对象
//		File file = new File(path + "/" + spuId + ".html");
		try {
			// 创建新文件
			if (file.createNewFile()) {
				System.out.println("文件创建成功：" + file.getAbsolutePath());
			} else {
				System.out.println("文件已存在：" + file.getAbsolutePath());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		PrintWriter writer = new PrintWriter(file, "UTF-8");
		// 4.执行合成生成
		templateEngine.process("item", context, writer);
	}
	
	@Override
	public void delHtml(String spuId) {
		// 3.指定文件生成后的存储路径
		File file = new File(pagepath, spuId + ".html");
		
		// 获取类加载器
//		ClassLoader classLoader = PageServiceImpl.class.getClassLoader();
		// 获取资源文件所在目录的路径
//		String path = classLoader.getResource("static").getPath();
		// 创建文件对象
//		File file = new File(path + "/" + spuId + ".html");
		
		// 获取路径
//		File file = new File(pagepath + spuId + ".html");
		// 删除文件
		file.delete();
	}
	
	/**
	 * 加载数据
	 */
	public Map<String, Object> loadData(String spuId){
		// 加载数据
		RespResult respResult = spuFeign.findProductById(spuId);
//		System.err.println(JSON.toJSONString(respResult));
		Object obj = respResult.getData();
		Product product = null;
		if(obj instanceof Product){
			product = (Product) obj;
		}
//		System.err.println(JSON.toJSONString(product));
		if(product != null){
			Map<String,Object> resultMap = new HashMap<>();
			Spu spu = product.getSpu();
			// spu
			resultMap.put("spu",spu);
			// 图片处理
			resultMap.put("images",spu.getImages().split(","));
			// 属性列表
			resultMap.put("attrs", JSON.parseObject(spu.getAttributeList(), Map.class));
			System.err.println(
				"------------spu属性列表----------\r\n"
				+JSON.parseObject(spu.getAttributeList(), Map.class)
				+"\r\n-------------------------------\r\n");
			
			// 查询三级分类
			RespResult<Category> one = categoryFeign.findById(spu.getCategoryOneId());
			RespResult<Category> two = categoryFeign.findById(spu.getCategoryTwoId());
			RespResult<Category> three = categoryFeign.findById(spu.getCategoryThreeId());
			// 装填三级分类
			resultMap.put("one",one.getData());
			resultMap.put("two",two.getData());
			resultMap.put("three",three.getData());
			
			// sku集合
			List<Map<String, Object>> skuList = new ArrayList<Map<String, Object>>();
			for(Sku sku : product.getSkus()){
				Map<String, Object> skuMap = new HashMap<>();
				skuMap.put("id",sku.getId());
				skuMap.put("name",sku.getName());
				skuMap.put("price",sku.getPrice());
				skuMap.put("attr", sku.getSkuAttribute());
				skuList.add(skuMap);
			}
			resultMap.put("skuList",skuList);
			return resultMap;
		}
		return null;
	}
	
}
